<?php
namespace app\admin\controller;

use app\common\model\AdminUser;
use think\Controller;
use utils\Config;
use utils\Result;

class Logins extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!empty(session('aid'))){
            header('location:'.url('index/index'));
            exit;
        }
    }

    public function index()
    {
        if ($this->request->isPost()){

            $param = $this->request->param();

            $info = AdminUser::get(['login_name' => $param['username'] ]);

            if (empty($info)) return Result::error('账号错误,请检查');

            if ($info->password !== password($param['password']) ) return Result::error('密码错误,请检查');

            session('aid',$info->id);

            return Result::success('登录成功')->put('url',url('index/index'));

        }
        $site_name = Config::get('site_name');

        $this->assign(compact('site_name'));

        return $this-> view -> fetch();
    }
}